<?php

function getv($name, $def = null)
{
    return get_array_item($_GET, $name, $def);
}

function postv($name, $def = null)
{
    return get_array_item($_POST, $name, $def);
}

function get_array_item($items, $key, $def = null)
{
    if(is_null($items)) return $def;
    if(!is_array($items)) return $def;
    if(isset($items[$key]))
        return $items[$key];
    else
        return $def;
}

function format_phone($phone)
{
    if(is_null($phone)) return false;
    if(empty($phone)) return false;
    if(preg_match("/^\+?\d(\(\d+\))?[\d\-\s]+$/", $phone) === 0) return false;
    
    $phone = str_replace("+", "", $phone);
    $phone = str_replace("(", "", $phone);
    $phone = str_replace(")", "", $phone);
    $phone = str_replace("-", "", $phone);
    $phone = str_replace(" ", "", $phone);
    
    return $phone;
}

function test_email($email)
{
    $regexp = "/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/";
    return preg_match($regexp, $email) > 0; 
}

